<template>
    <div>
        <div class="operation">
            <a class="btn btn-theme" @click="showModifyModal">编辑维保申请</a>
            <a class="btn btn-theme" @click="showAddModal">新建维保申请</a>
            <a class="btn btn-theme" @click="showDeleteModal">删除维保申请</a>
        </div>
        <table id="fix_table" class="table">
        </table>
         <div class="modal fade" id="fix_ok_bx_modal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                        </div>
                        <div class="modal-body">
                            确认修好？该操作不可返回！
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" @click="update">确认</button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
            </div>
              <div class="modal fade" id="fixDeleteModal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                        </div>
                        <div class="modal-body">
                            确认删除？该操作不可返回！
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" @click="Delete">确认</button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
            </div>
                <div class="modal fade" id="fix_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="exampleModalLabel">添加维修申请</h4>
                        </div>
                        <div class="modal-body">
                            <form>
                                <div class="form-group">
                                    <label for="add_fix_assets" class="control-label">维修物品：</label>
                                    <select type="text" class="form-control" id="add_fix_assets"></select>
                                </div>
                                <div class="form-group">
                                    <label for="add_fix_dept" class="control-label">维修部门：</label>
                                    <select type="text" class="form-control" id="add_fix_dept"></select>
                                </div>
                                <div class="form-group">
                                    <label for="add_fix_ftype" class="control-label">维修/保养：</label>
                                    <select type="text" class="form-control" id="add_fix_ftype">
										<option value="大维修">大维修</option>
										<option value="小维修">小维修</option>
										<option value="大保养">大保养</option>
										<option value="小保养">小保养</option>
                                        <option value="外修">外修</option>
									</select>
                                </div>
                                <div class="form-group">
                                    <label for="explain_add" class="control-label">故障说明</label>
                                    <textarea id="explain_add" class="form-control" rows="3"></textarea>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary" @click="addSubmit">提交</button>
                        </div>
                    </div>
                </div>
            </div>
               <div class="modal fade" id="fix_modify_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="exampleModalLabel">编辑维修申请</h4>
                        </div>
                        <div class="modal-body">
                            <form>
                                <div class="form-group">
                                    <label for="modify_fix_assets" class="control-label">维修物品：</label>
                                    <select type="text" class="form-control" id="modify_fix_assets"></select>
                                </div>
                                <div class="form-group">
                                    <label for="modify_fix_dept" class="control-label">维修部门：</label>
                                    <select type="text" class="form-control" id="modify_fix_dept"></select>
                                </div>
                                <div class="form-group">
                                    <label for="modify_fix_ftype" class="control-label">维修/保养：</label>
                                    <select type="text" class="form-control" id="modify_fix_ftype">
										<option value="大维修">大维修</option>
										<option value="小维修">小维修</option>
										<option value="大保养">大保养</option>
										<option value="小保养">小保养</option>
                                        <option value="外修">外修</option>
									</select>
                                </div>
                                <div class="form-group">
                                    <label for="explain_modify" class="control-label">故障说明</label>
                                    <textarea id="explain_modify" class="form-control" rows="3"></textarea>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary" @click="modifySubmit">提交</button>
                        </div>
                    </div>
                </div>
            </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                currentRow: ''
            }
        },
        methods: {
            getFixTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assetsfix/fetchallfixbyuserid',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'extra.selectfield': '*',
                        'extra.sortfield': 'fidno',
                        'extra.pagesize': 99999999,
                        'extra.pageindex': 1,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {

                        if (retdata.Table1[0].status == true) {
                            for (var i = 0; i < retdata.Table2.length; i++) {
                                retdata.Table2[i].fentdte = retdata.Table2[i].fentdte.substring(0, 10)
                                retdata.Table2[i].fixokdate = retdata.Table2[i].fixokdate.substring(0, 10)
                                retdata.Table2[i].fokdate = retdata.Table2[i].fokdate.substring(0, 10)
                                if (retdata.Table2[i].fixok == true) {
                                    retdata.Table2[i].fixok = '是'
                                }
                                if (retdata.Table2[i].fixok == false) {
                                    retdata.Table2[i].fixok = '否'
                                }
                                if (retdata.Table2[i].fisok == true) {
                                    retdata.Table2[i].fisok = '是'
                                }
                                if (retdata.Table2[i].fisok == false) {
                                    retdata.Table2[i].fisok = '否'
                                }


                            }
                        }
                        var operateEvents = {
                            'click a[name="apply"]': function(elem, value, row, index) {
                                _this.currentRow = row
                                _this.showTipModal()
                            }
                        }
                        $('#fix_table').bootstrapTable('destroy')
                        $('#fix_table').bootstrapTable({
                            columns: [{
                                radio: true
                            }, {
                                field: 'fidno',
                                title: '维保单号',
                                halign: 'center'
                            }, {
                                field: 'fassetsid',
                                title: '资产编号',
                                class: 'hidden'
                            }, {
                                field: 'fbarcode',
                                title: '物料型号',
                                halign: 'center',
                                class: 'hidden'
                            }, {
                                field: 'fname',
                                title: '维保物品名',
                                halign: 'center'
                            }, {
                                field: 'fdescription',
                                title: '故障描述',
                                halign: 'center'
                            }, {
                                field: 'fdname',
                                title: '维保部门',
                                halign: 'center'
                            }, {
                                field: 'ftype',
                                title: '维保类型',
                                halign: "center"
                            }, {
                                field: 'fentdte',
                                title: '报修日期',
                                halign: 'center'
                            }, {
                                field: 'fixok',
                                title: '是否维保完毕',
                                halign: 'center'
                            }, {
                                field: 'fixokdate',
                                title: '维保完毕时间',
                                halign: 'center'
                            }, {
                                field: 'fisok',
                                title: '是否确认修好',
                                halign: 'center'
                            }, {
                                field: 'fokdate',
                                title: '确认修好时间',
                                halign: 'center'
                            }, {
                                field: 'operate',
                                title: '操作',
                                halign: 'center',
                                formatter: '<a class="btn btn-success btn-sm" name="apply">确认修好</a>',
                                events: operateEvents
                            }],
                            data: retdata.Table2,
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100, 200, 500],
                            smartDisplay: true,
                            selectItemName: 'assets',
                            showHeader: true,
                            maintainSelected: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                        })
                    }
                });
            },
            showTipModal() {
                if (this.currentRow.fixok == '是' && this.currentRow.fisok == '否') {
                    $('#fix_ok_bx_modal').modal('show')
                } else if (this.currentRow.fixok == '否') {
                    this.$store.commit('showModal','需要等待维修部门修好确认')
                  
                } else {
                    this.$store.commit('showModal','该条维修单禁止操作')
                    
                }
            },
            showAddModal() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/fetchlistbydeptidno',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        if (retdata.Table1[0].status == false) {
                        _this.$store.commit('showModal','当前部门无可修物品')
                            $('#fix_add_modal').modal('hide');
                        } else {
                            var cont = '';
                            for (var i = 0; i < retdata.Table2.length; i++) {
                                cont = cont + '<option value="' + retdata.Table2[i].fidno + '">' + retdata.Table2[i].fbarcode + ':' + retdata.Table2[i].fname + '</option>';
                            }
                            $('#add_fix_assets').html(cont);

                        }
                    }
                });

                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        var cont = '';
                        for (var i = 0; i < retdata.Table2.length; i++) {
                            cont = cont + '<option value="' + retdata.Table2[i].fidno + '">' + retdata.Table2[i].fdname + '</option>';
                        }
                        $('#add_fix_dept').html(cont);
                    }
                });
                $('#fix_add_modal').modal('show')
            },
            showModifyModal() {
                var _this = this
                var ids = $('input[name="assets"]:checked').parent().next().text();
                if ($('input[name="assets"]:checked').parent().parent().children().eq(9).text() == '是') {
                    _this.$store.commit('showModal','已修好，不能修改！')
                } else {

                    var fassetsid = $('input[name="assets"]:checked').parent().parent().children().eq(2).text();
                    var barcode = $('input[name="assets"]:checked').parent().parent().children().eq(3).text();
                    var fname = $('input[name="assets"]:checked').parent().parent().children().eq(4).text();
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/assets/fetchlistbydeptidno',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid
                        },
                        dataType: 'json',
                        crossDomain: true,
                        async: true,
                        error: function(request) {
                            _this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {
                            if (retdata.Table1[0].status == false) {
                                var cont = '';

                                cont = cont + '<option value="' + fassetsid + '">' + barcode + ':' + fname + '</option>';
                                $('#modify_fix_assets').html(cont);
                            } else {
                                var cont = '';
                                for (var i = 0; i < retdata.Table2.length; i++) {
                                    cont = cont + '<option value="' + retdata.Table2[i].fidno + '">' + retdata.Table2[i].fbarcode + ':' + retdata.Table2[i].fname + '</option>';
                                }
                                cont = cont + '<option value="' + fassetsid + '">' + barcode + ':' + fname + '</option>';
                                $('#modify_fix_assets').html(cont);

                            }
                        }
                    });

                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/dept/fetchalllist',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'teamid': localStorage.activeteamid
                        },
                        dataType: 'json',
                        crossDomain: true,
                        async: true,
                        error: function(request) {
                            _this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {

                            var cont = '';
                            for (var i = 0; i < retdata.Table2.length; i++) {
                                cont = cont + '<option value="' + retdata.Table2[i].fidno + '">' + retdata.Table2[i].fdname + '</option>';
                            }
                            $('#modify_fix_dept').html(cont);
                        }
                    });
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/assetsfix/fetchallfixbyuserid',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'teamid': localStorage.activeteamid,
                            'extra.selectfield': '*',
                            'extra.sortfield': 'fidno',
                            'extra.pagesize': '999999',
                            'extra.pageindex': '1',
                        },
                        dataType: 'json',
                        crossDomain: true,
                        error: function(request) {
                            _this.$store.commit('showModal','连接错误')
                        },

                        success: function(retdata) {
                            for (var i = 0; i < retdata.Table2.length; i++) {
                                if (ids == retdata.Table2[i].fidno) {
                                    $('#modify_fix_assets').val(retdata.Table2[i].fassetsid);
                                    $('#modify_fix_dept').val(retdata.Table2[i].fdeptid);
                                    $('#explain_modify').val(retdata.Table2[i].fdescription);
                                }
                            }
                        }
                    });
                    if (ids != '') {

                        $('#fix_modify_modal').modal('show');
                    } else {
                        _this.$store.commit('showModal','先选择维修单')
                    }
                }
            },
            update() {
                var _this = this
                var mydate = this.getnowtime
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assetsfix/update',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fidno': _this.currentRow.fidno,
                        'fassetsid': _this.currentRow.fassetsid,
                        'ftype': _this.currentRow.ftype,
                        'fdescription': _this.currentRow.fdescription,
                        'fdeptid': _this.currentRow.fdeptid,
                        'fixok': true,
                        'fixokdate': _this.currentRow.fixokdate,
                        'fisok': true,
                        'fokdate': mydate,
                        'fixnotes': '',
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },

                    success: function(retdata) {
                        $('#fix_ok_bx_modal').modal('hide')
                        _this.getFixTable()
                    }
                });
            },
            addSubmit() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assetsfix/add',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fassetsid': $('#add_fix_assets').val(),
                        'fdeptid': $('#add_fix_dept').val(),
                        'ftype': $('#add_fix_ftype').val(),
                        'fdescription': $('#explain_add').val(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#fix_add_modal').modal('hide')
                        _this.getFixTable()
                    }
                });
            },
            modifySubmit() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assetsfix/update',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fidno': $('input[name="assets"]:checked').parent().next().text(),
                        'fassetsid': $('#modify_fix_assets').val(),
                        'fdeptid': $('#modify_fix_dept').val(),
                        'ftype': $('#modify_fix_ftype').val(),
                        'fdescription': $('#explain_modify').val(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#fix_modify_modal').modal('hide')
                        _this.getFixTable()
                    }
                });
            },
            showDeleteModal() {
                $('#fixDeleteModal').modal('show');

            },
            Delete() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assetsfix/delete',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fidno': $('input[name="assets"]:checked').parent().next().text(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#fixDeleteModal').modal('hide')
                        _this.getFixTable()
                    }
                });
            },
            getnowtime() {
                var nowtime = new Date();
                var year = nowtime.getFullYear();
                var month = this.padleft0(nowtime.getMonth() + 1);
                var day = this.padleft0(nowtime.getDate());
                var hour = this.padleft0(nowtime.getHours());
                var minute = this.padleft0(nowtime.getMinutes());
                var second = this.padleft0(nowtime.getSeconds());
                var millisecond = nowtime.getMilliseconds();
                millisecond = millisecond.toString().length == 1 ? "00" + millisecond : millisecond.toString().length == 2 ? "0" + millisecond : millisecond;
                return year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + "." + millisecond;
            },
            //补齐两位数  
            padleft0(obj) {
                return obj.toString().replace(/^[0-9]{1}$/, "0" + obj);
            }
        },
        mounted() {
            this.getFixTable()
        }
    }
</script>
<style>

</style>